//
//  GGCarScrollView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/30.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGCarScrollView.h"
@interface GGCarScrollView ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *ggScrollView;
@property (strong, nonatomic) UIPageControl *ggPageCon;

@end

@implementation GGCarScrollView
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{

    UIScrollView *ggScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    /**
     此处是scrollView的顶部空白问题 （stateView高度距离）
     如不进行判断，scrollView不会置顶展示
     */
    if (@available(iOS 11.0, *)) {
        ggScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
//        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.ggScrollView = ggScrollView;
    ggScrollView.delegate = self;
    [self addSubview:ggScrollView];
    //相关设置
    ggScrollView.showsHorizontalScrollIndicator = NO;
    ggScrollView.showsVerticalScrollIndicator = NO;
    ggScrollView.bounces = NO;
    //开启分页
    ggScrollView.pagingEnabled = YES;
    
//仅仅做展示使用
    UIPageControl *ggPageCon = [[UIPageControl alloc]init];
    self.ggPageCon = ggPageCon;
    //禁止用户交互
    ggPageCon.userInteractionEnabled = NO;
    //当前页与非当前页的颜色
    ggPageCon.pageIndicatorTintColor = [UIColor redColor];
    ggPageCon.currentPageIndicatorTintColor = [UIColor blueColor];
    
    [self addSubview:self.ggPageCon];
    [ggPageCon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-20);
           make.centerX.equalTo(self);
    }];
    
}
#pragma mark - 类似于setmodel的用法，对于自定义的view而言
- (void)setImageNameList:(NSArray<NSString *> *)imageNameList{
    _imageNameList = imageNameList;
    for (NSInteger i= 0; i < imageNameList.count; i ++) {
        //添加图片
        UIImageView *imaView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_imageNameList[i]]];
        [self.ggScrollView addSubview:imaView];
        imaView.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height);
        imaView.userInteractionEnabled = YES;//可以与用户交互
        
        //每张图片上都添加一按钮
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageNamed:@"common_more_black"] forState:UIControlStateNormal];//默认
        [button setBackgroundImage:[UIImage imageNamed:@"common_more_white"] forState:UIControlStateHighlighted];//高亮
        [imaView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.offset(-20);
            make.size.mas_equalTo(CGSizeMake(80, 40));
        }];
        [button addTarget:self action:@selector(clickMore) forControlEvents:UIControlEventTouchUpInside];
    }
    //设置scrollView的内容
    self.ggScrollView.contentSize = CGSizeMake((_imageNameList.count + 1)* self.frame.size.width, self.frame.size.height);
//    self.ggPageCon.currentPage = 0;
    self.ggPageCon.numberOfPages = _imageNameList.count;
}
#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //获取scrollView滚动距离
    CGFloat offsetx = scrollView.contentOffset.x;//偏移量
    NSInteger page = round(offsetx /self.frame.size.width);//四舍五入
    self.ggPageCon.currentPage = page;
    //如果当前页数等于page隐藏
    self.ggPageCon.hidden = (page == self.imageNameList.count);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //此处是滑动动作停止了就会调用的方法
    //判断是否已经全部滑动完，如果是，隐藏
    if(round(scrollView.contentOffset.x / self.frame.size.width) == _imageNameList.count)
       [self removeFromSuperview];
}
//scrollView滚动的时候就会调用这个方法
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
////    NSLog(@"offset = %@",NSStringFromCGPoint(scrollView.contentOffset));
//}
//scrollView将要开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"scrollViewWillBeginDragging");
}
//scrollView结束拖拽的时候调用.但是,此时并不代表scrollView已经停止滚动了!!!
//如果 decelerate = 0 此时scrollView就已经停止滚动了
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) {
        NSLog(@"scrollView已经停止了");
    }
    NSLog(@"scrollViewDidEndDragging");
}

//scrollView将要开始减速的时候调用.这个方法并不一定会被调用.如果这个方法被调用了,说明手指已经离开了,而且还在滚动
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"scrollViewWillBeginDecelerating");
}
//scrollView结束减速的时候调用.这个方法也不一定会被调用,如果它被调用了,scrollView的滚动肯定就已经停止
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"scrollViewDidEndDecelerating");
//}

//如果使用动画修改scrollView的偏移量,当scrollView滚动停止的时候,会调用这个方法.
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidEndScrollingAnimation");
}

//告诉scrollView,它的哪个子控件可以进行缩放
//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//    //
//    return _picture1;
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint offset = self.ggScrollView.contentOffset;
    offset.x += self.frame.size.width;
    //滚动偏移量 手动修改
    [self.ggScrollView setContentOffset:offset animated:YES];
}

- (void)clickMore{
    /**
     视图放大两倍，变为透明
     */
    [UIView animateWithDuration:2 animations:^{
        self.transform = CGAffineTransformMakeScale(2, 2);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
/*
 1.创建的时候不用调用.
 2.当控件被添加到父控件的时候,会被调用.
 3.当控件里添加了子控件的时候,会被调用.
 4.当子控件的frame发生变化的时候,会被调用.

 这个方法调用的时候,view就一定有大小了
 */
- (void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mark  - lazy
//- (UIPageControl *)ggPageCon{
//    if(!_ggPageCon){
//        _ggPageCon = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 70, CGRectGetWidth(self.frame), 70)];
//        _ggPageCon.userInteractionEnabled  = YES;
//        _ggPageCon.currentPage = 0;
//        _ggPageCon.numberOfPages = _imageNameList.count;
//    }
//    return _ggPageCon;
//}


@end
